package yl.hs.bmipfull.service.salemanage.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.laboratory.IProductInfoDao;
import yl.hs.bmipfull.daos.salemanage.IContractPriceAdjustmentDao;
import yl.hs.bmipfull.daos.salemanage.ISaleContractProductsDao;
import yl.hs.bmipfull.pojo.salemanage.ContractPriceAdjustmentEntity;
import yl.hs.bmipfull.service.salemanage.IContractPriceAdjustmentService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 作用:  销售合同调价单明细信服务层接口实现<br/>
 * 创建时间: 2021年06月03日 10时05分28秒 <br/>
 *
 * @author 张凯
 */
@Service
public class ContractPriceAdjustmentServiceImpl extends SaleBaseService implements IContractPriceAdjustmentService {

    @Autowired
    IContractPriceAdjustmentDao dao;
    @Autowired
    ISaleContractProductsDao contractProductsDao;

    @Override
    public List<Map<String,Object>> queryList(PageQuery pageQuery) throws Exception {
        List<Map<String,Object>> result=new ArrayList<>();
        pageQuery.checkSortSides(ContractPriceAdjustmentEntity.class);
        var list=dao.queryList(pageQuery.getQuery());
        for (var entity:list) {
            var map=entity.toMap();
            var product=contractProductsDao.findBySN(entity.getContractProductSN());
            map.put("productName",product.getProductName()+"/"+product.getProductSpec());
            result.add(map);
        }
        return result;
    }


    @Override
    public ContractPriceAdjustmentEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional
    public Integer saveContractPriceAdjustment(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        ContractPriceAdjustmentEntity entity = new ContractPriceAdjustmentEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(ContractPriceAdjustmentEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入销售合同调价单明细信", Tl.toJson(entity), ContractPriceAdjustmentEntity.class);
            }
        return result;
    }

    @Override
    @Transactional
    public Integer delete(String[] sn) {
        // TODO 2021年6月3日15:03:16: 删除功能
        return 0;
    }

    @Override
    public Integer saveContractPriceAdjustment(List<Map<String, Object>> adjustment, String docSn) throws Exception {
        Integer result = 0;
        removeByAdjustDocSN(docSn);
        for (Map<String, Object> model : adjustment) {
            model.put("adjustDocSN", docSn);
            result += saveContractPriceAdjustment(model, model.get("sn").toString());
        }
        return result;
    }


    @Override
    public float getProductPrice(String contractProductSn, Date dateTime) {

        var data = dao.getProductPrice(contractProductSn, dateTime);
        return data == null ? 0.00f :data;
    }

    @Override
    public boolean removeByAdjustDocSN(String adjustDocSN) {

        try {
            dao.removeByAdjustDocSn(adjustDocSN);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}